﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using System.Data;
using Yipusoft.Duty.Model;

public partial class BasicBase_Duty_AddMember : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Bind();
    }
    private void Bind()
    {
        string strSql  = string.Empty;
        string strSql1 = string.Empty;
        string DutyType = Request["DutyType"];
        bool Flag      = false; 
        string[] Department     = { "司令部】", "政治部】", "后勤部】", "防火部】" };
        string DepartmentName   = string.Empty;
        string DeptID           = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        DepartmentName          = Yipusoft.Account.BLL.Department.GetDepartment(DeptID).AliasName;

        if (DeptID == null || DeptID.Length < 5)
            return;

        if (DeptID.Substring(4, 1).Equals("0"))
        {
            strSql  = " Left(DepartmentID,5)='" + DeptID.Substring(0, 5) + "'";            
            Flag = true;
        }
        else
        {
            strSql  = " DepartmentID='" + DeptID + "'";
           
        }        
       
        TableRow Trw;
        TableCell Cell;
        DataSet dst = Yipusoft.Duty.BLL.Member.GetList(" UserID in(Select UserID From MemberStation Where DutyType='"+DutyType+"' And "+strSql+")");
        if (Flag)
        {
            for (int i = 0; i <= 3; i++)
            {
                Trw = new TableRow();
                Cell = new TableCell();
                Cell.CssClass = "tabstyle";
                Cell.ColumnSpan = 5;
                Cell.Height = 26;
                Cell.HorizontalAlign = HorizontalAlign.Left;
                Cell.Controls.Add(new LiteralControl("<font color=Orange>【" + DepartmentName + Department[i].ToString() + "</font>"));
                Trw.Cells.Add(Cell);

                strSql1 = " Left(DepartmentID,6)='" + DeptID.Substring(0, 5) + (i + 1).ToString() + "'";              
                TabList.Rows.Add(Trw);
                AddCell(DeptID, strSql1, dst);
            }
        }
        else
        {
            Trw = new TableRow();
            Cell = new TableCell();
            Cell.CssClass = "tabstyle";
            Cell.ColumnSpan = 5;
            Cell.HorizontalAlign = HorizontalAlign.Left;
            Cell.Controls.Add(new LiteralControl("<font color=Orange>【" + DepartmentName + "支队人员列表" + "】</font>"));
            Trw.Cells.Add(Cell);
            TabList.Rows.Add(Trw);
            AddCell(DeptID, strSql, dst);
        }
       

     }
 

    private void AddCell(string Dpt,string strWhere,DataSet dst)
    { 
        TableRow  Trw=null;        
        TableCell Cell;
        List<MemberUserInfo> list = Yipusoft.Duty.BLL.MemberUser.GetList(strWhere);        
        int Len = list.Count%6>0?(list.Count/6)+1:(list.Count/6);
        int index=0;
        for (int i = 0; i < Len; i++)
        {
            Trw = new TableRow();
            TabList.Rows.Add(Trw);
            for (int n = 0; n < 6; n++)
            {

                if (index < list.Count)
                {
                    Cell = new TableCell();
                    Cell.CssClass = "tabstyle";
                    Cell.HorizontalAlign = HorizontalAlign.Left;
                    CheckBox Box = new CheckBox();
                    Box.ID = list[index].UserID;
                    Box.Text = list[index].UserName;
                    Box.Checked = dst.Tables[0].Select(" UserID='" + list[index].UserID + "'").Length > 0 ? true : false;
                    Cell.Attributes["IsCheck"] = Box.Checked.ToString();                    
                    Cell.Controls.Add(Box);
                    Cell.Width = 90;
                    Trw.Cells.Add(Cell);
                }
                else
                {
                    Cell = new TableCell();
                    Cell.CssClass = "tabstyle";
                    Cell.Width = 90;
                    Trw.Cells.Add(Cell);
                }
             
                index++;
            }
            
        }    
        
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        UserInfo user = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);

        for (int i = 1; i < TabList.Rows.Count; i++)
        {
            for (int n = 0; n < TabList.Rows[i].Cells.Count; n++)
            {
                if (TabList.Rows[i].Cells[n].HasControls())
                {
                    if (TabList.Rows[i].Cells[n].Controls[0] is CheckBox)
                    {
                        CheckBox Box = TabList.Rows[i].Cells[n].Controls[0] as CheckBox;
                        if (bool.Parse(TabList.Rows[i].Cells[n].Attributes["IsCheck"].ToString()) != Box.Checked)
                        {

                            if (Box.Checked)
                            {
                                Yipusoft.Duty.Model.MemberInfo Member = new Yipusoft.Duty.Model.MemberInfo();
                                Yipusoft.Duty.Model.MemberStationInfo Model = new Yipusoft.Duty.Model.MemberStationInfo();
                                string DepartmentID = Yipusoft.Duty.BLL.MemberUser.GetModel(Box.ID).DepartmentID;

                                Member.IsDelete = false;
                                Member.Operator = user.UserName;
                                Member.Updatetime = DateTime.Now;
                                Member.UserID = Box.ID;
                                Member.UserName = Box.Text;
                                Member.Station = "";
                                Member.OperatorID = user.UserID;
                                Member.DepartmentID = DepartmentID;
                                Yipusoft.Duty.BLL.Member.Add(Member);


                                Model.DepartmentID = DepartmentID;
                                Model.DutyType = Request["DutyType"];
                                Model.UserID = Box.ID;
                                Model.UserName = Box.Text;
                                Yipusoft.Duty.BLL.MemberStation.Add(Model);
                            }
                            else
                            {
                                Yipusoft.Duty.BLL.Member.Delete(Box.ID);
                                Yipusoft.Duty.BLL.MemberStation.Delete(Box.ID);
                            }


                        }
                        else
                        {

                        }
                    }
                }
            }
        }

    }
}
